import { _decorator, Component, Node, BlockInputEvents, UIOpacity, Label, Color, Prefab, instantiate, UITransform, tween, Tween, Vec3 } from 'cc';
import { PopupManager } from '../managers/PopupManager';
import { PopupConfig } from './GlobalManager';
const { ccclass, property } = _decorator;
@ccclass('GlobalUIManager')
export class GlobalUIManager extends Component {

    @property({type:Prefab,displayName:"屏蔽层"})
    blockLayerPre: Prefab = null;
    @property({type:Prefab,displayName:"提示层"})
    tipsLayerPre: Prefab = null;
    /**弹窗层 */
    popupLayer: Node = null!;
    /**阻塞层 */
    blockLayer: Node = null!;
    /**提示层 */
    tipsLayer: Node = null!;


    onLoad() {
        console.log("GlobalUIManager===============")
        
       
    }

    init(){
          // 设置静态单例引用
        // 层可通过编辑器挂载
        // 保证三层在父节点最顶层，且顺序为 tipsLayer > blockLayer > popupLayer
        if(!this.popupLayer){
            this.popupLayer = new Node("popupLayer");
            this.popupLayer.parent = this.node
            this.popupLayer.addComponent(UITransform)
        }
        
        if(!this.blockLayer){
            this.blockLayer = instantiate(this.blockLayerPre);
            this.blockLayer.parent = this.node
            this.blockLayer.active = false
        }
        if(!this.tipsLayer){
            this.tipsLayer = instantiate(this.tipsLayerPre);
            this.tipsLayer.parent =this.node
            this.tipsLayer.getChildByName("bgColor")!.active = false;
        }

      
        
        
        this.popupLayer.getComponent(UITransform)!.width = this.node.getComponent(UITransform)!.width;
        this.popupLayer.getComponent(UITransform)!.height = this.node.getComponent(UITransform)!.height;
        this.popupLayer.setSiblingIndex(1);
        this.blockLayer.setSiblingIndex(2);
        this.tipsLayer.setSiblingIndex(3);
    }

    onDestroy() {
       
    }


   
    // ========== 实例方法 ==========

    public showBlockLayer(opacity: number = 200) {
        if (!this.blockLayer) return;
        this.blockLayer.active = true;
        let blockerOpacity = this.blockLayer.getChildByName("bgColor")!.getComponent(UIOpacity)!;
        blockerOpacity.opacity = opacity;
        if (!this.blockLayer.getComponent(BlockInputEvents)) {
            this.blockLayer.addComponent(BlockInputEvents);
        }
    }

    public hideBlockLayer() {
        if (this.blockLayer) this.blockLayer.active = false;
    }

    public showTips(msg: string, duration: number = 1, color: Color = new Color(255, 255, 255)) {
        if (!this.tipsLayer) return;
        let label = this.tipsLayer.getChildByName("bgColor")!.getChildByName("TipsLb")!.getComponent(Label)!;
        this.tipsLayer.getChildByName("bgColor")!.active = true;
        label.string = msg;
        label.color = color;
        this.tipsLayer.getChildByName("bgColor")!.setPosition(0, 0, 0);
        Tween.stopAllByTarget(this.tipsLayer.getChildByName("bgColor")!);
        tween().target(this.tipsLayer.getChildByName("bgColor")!)
            .to(duration, { position: new Vec3(0, 0, 0) })
            .to(0.5, { position: new Vec3(0, 100, 0) })
            .call(() => {
                this.tipsLayer.getChildByName("bgColor")!.active = false;
            })
            .start();
    }

    /**
     * 通过 PopConfig 配置弹窗，自动区分本地/分包，参数合并，统一交给 PopupManager
     * @param popConfig PopConfig 配置项
     */
    public async showPopup(popConfig: PopupConfig): Promise<Node | null> {
        if (!this.popupLayer) return null;
        return await PopupManager.instance.showPopup(popConfig, this.popupLayer);
    }

    public closePopup(config: PopupConfig) {
        PopupManager.instance.closePopup(config);
    }
} 